package com.sky.mapper;

import com.github.pagehelper.Page;
import com.sky.dto.OrdersPageQueryDTO;
import com.sky.entity.OrderStatistic;
import com.sky.entity.Orders;
import com.sky.entity.TurnoverStatistic;
import org.apache.ibatis.annotations.MapKey;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

@Mapper
public interface OrderMapper {

    /**
     * 插入订单数据
     * @param orders
     * @return
     */
    void insert(Orders orders);


    /**
     * 根据订单号查询订单
     * @param orderNumber
     */
    @Select("select * from orders where number = #{orderNumber}")
    Orders getByNumber(String orderNumber);


    /**
     * 修改订单信息
     * @param orders
     */
    void update(Orders orders);

    /**
     * 根据订单号修改订单状态
     * @param orderNumber
     * @param status
     * @param payStatus
     * @param checkoutTime
     */
    @Update("update orders set status = #{status}, pay_status = #{payStatus}, checkout_time = #{checkoutTime} where number = #{orderNumber}")
    void updateOrderStatus(String orderNumber, Integer status, Integer payStatus, LocalDateTime checkoutTime);

    /**
     * 分页查询
     * @param orderPageQueryDTO
     * @return
     */
    Page<Orders> pageQuery(OrdersPageQueryDTO orderPageQueryDTO);


    /**
     * 统计不同状态订单数量
     * @param status
     * @return
     */
    @Select("select count(*) from orders where status = #{status}")
    Integer countStatus(Integer status);


    /**
     * 根据id查询订单
     * @param id
     * @return
     */
    @Select("select * from orders where id = #{id}")
    Orders getById(Long id);


    /**
     * 根据状态和下单时间查询订单
     * @param status
     * @param time
     * @return
     */
    List<Orders> getByStatusAndOrderTimeLT(Integer status, LocalDateTime time);

    /**
     * 统计指定时间区间内的营业额数据
     * @param begin
     * @param end
     * @return
     */
    @Select("select DATE(delivery_time) as day , SUM(amount) as amount FROM orders " +
            "WHERE delivery_time BETWEEN #{begin} AND #{end} AND `status` = 5 GROUP BY day")
    List<TurnoverStatistic> getDayAndAmountByDate(LocalDate begin, LocalDate end);


    /**
     * 统计指定时间区间内的订单数据
     * @param begin
     * @param end
     * @param status
     * @return
     */
    List<OrderStatistic> getOrderListByDateAndStatus(LocalDate begin, LocalDate end, Integer status);
}
